package com.xiaoming;

import com.xiaoming.entity.DatabaseConfig;
import com.xiaoming.zk.CuratorServer;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Homework3Bootstrap {
    public static HikariDataSource dataSource = null;

    public static void main(String[] args) throws Exception {
        CuratorServer.getInstance().build(Homework3Bootstrap.class, (Object instance) -> {
            if (instance instanceof DatabaseConfig) {
                DatabaseConfig config = (DatabaseConfig) instance;
                HikariConfig hikariConfig = new HikariConfig();
                hikariConfig.setJdbcUrl(config.getJdbcUrl());
                hikariConfig.setDriverClassName(config.getDriverClassName());
                hikariConfig.setUsername(config.getUsername());
                hikariConfig.setPassword(config.getPassword());
                dataSource = new HikariDataSource(hikariConfig);
            }
        });
        Homework3Bootstrap.setDatabase();
        SpringApplication.run(Homework3Bootstrap.class);
    }

    /***
     * 设置数据源配置
     * @throws Exception
     */
    private static void setDatabase() throws Exception {
        DatabaseConfig database = new DatabaseConfig();
        database.setJdbcUrl("jdbc:mysql://192.168.92.2:3398/testa?characterEncoding=utf8&useSSL=false");
        database.setUsername("root");
        database.setPassword("123456");
        database.setDriverClassName("com.mysql.jdbc.Driver");
        CuratorServer.getInstance().setData(database);
    }
}
